package com.woniu.service.impl;

import com.woniu.entity.Emprole1;
import com.woniu.mapper.EmproleDao;
import com.woniu.mapper.RoleDao;
import com.woniu.pojo.Emprole;
import com.woniu.pojo.EmproleExample;
import com.woniu.pojo.Role;
import com.woniu.pojo.RoleExample;
import com.woniu.service.EmpRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class EmpRoleServiceImpl implements EmpRoleService {

    @Autowired
    EmproleDao emproleDao;
    @Autowired
    RoleDao roleDao;

    /**
     * 给用户分配角色
     * 传进员工ID和角色I
     * */
    @Override
    public void add(Emprole emprole) {
        emproleDao.insert(emprole);
    }
    /**
     * 通过ID删除角色
     * */
    @Override
    public void deleteByExample(Emprole emprole) {
        EmproleExample emproleExample=new EmproleExample();
        emproleExample.createCriteria().andEmpIdEqualTo(emprole.getEmpId()).andRoleIdEqualTo(emprole.getRoleId());
        emproleDao.deleteByExample(emproleExample);
    }

    /**
     * 通过员工ID查询已分配的角色
     * 返回已分配的角色
     * */
    @Override
    public List<Emprole1> findIsEmpRole(Integer empId) {
        if(empId!=null){
            EmproleExample  emproleExample=new EmproleExample();
            emproleExample.createCriteria().andEmpIdEqualTo(empId);
            List<Emprole1> emprole1s = emproleDao.selectByExample1(emproleExample);
            return emprole1s;
        }else {
            return null;
        }


    }

    /**
     * 通过员工ID查询未分配的角色
     * 返回未分配的角色
     * */
    @Override
    public List<Role> findNoEmpRole(Integer EmpId) {
        List<Emprole1> isEmpRole = findIsEmpRole(EmpId);
        List<Integer> nums=new ArrayList<>();
        for (Emprole1 e : isEmpRole) {
            System.out.println("已分配角色"+e.getRoleId());
            nums.add(e.getRoleId().getRoleId());
        }
        RoleExample roleExample=new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if (nums.size()>0) {
            criteria.andRoleIdNotIn(nums);
        }
        criteria.andRoleStatusEqualTo("1");
        List<Role> roles = roleDao.selectByExample(roleExample);
        for (Role e : roles) {
            System.out.println("未分配角色"+e);
        }

        return roles;
    }

    /**
     * 根据角色ID查询用户
     * */
    @Override
    public List<Emprole1> findByRoleId(Integer roleId) {
        EmproleExample  emproleExample=new EmproleExample();
        emproleExample.createCriteria().andRoleIdEqualTo(roleId);
        List<Emprole1> emprole1s = emproleDao.selectByExample1(emproleExample);
        return emprole1s;
    }


}
